import { createRouter, createWebHashHistory } from "vue-router";

import Home from '@/pages/index.vue'
import Login from '@/pages/login.vue'
import NotFound from '@/pages/404.vue'
import Layout from '@/layouts/Index.vue'
import GoodList from '@/pages/goods/Index.vue'
import Comment from '@/pages/comment/Index.vue'
import Coupon from '@/pages/coupon/Index.vue'
import Image from '@/pages/image/Index.vue'
import Notice from '@/pages/notice/Index.vue'
import Order from '@/pages/order/Index.vue'
import Setting from '@/pages/setting/Index.vue'
import User from '@/pages/user/Index.vue'
import CateGory from '@/pages/category/Index.vue'
import Manager from '@/pages/manager/Index.vue'
import Access from '@/pages/access/Index.vue'
import Role from '@/pages/role/Index.vue'
import Skus from '@/pages/skus/Index.vue'
import Level from '@/pages/level/Index.vue'
import SettingBuy from '@/pages/setting/buy.vue'
import SettingShip from '@/pages/setting/ship.vue'
import DistributionIndex from '@/pages/distribution/index.vue'
import DistributionSetting from '@/pages/distribution/setting.vue'

// 默认路由所有用户共享
const routes = [
    {
        path: '/',
        name: 'admin',
        component: Layout,
    }, {
        path: '/login',
        meta: {
            title: '登录'
        },
        component: Login
    }, {
        path: '/:pathMatch(.*)*',
        meta: {
            title: '404'
        },
        name: 'NotFound',
        component: NotFound
    }]

// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes = [
    {
        path: '/',
        name: '/',
        meta: {
            title: '后台首页'
        },
        component: Home
    }, {
        path: '/goods/list',
        name: '/goods/list',
        meta: {
            title: '商品管理'
        },
        component: GoodList
    }, {
        path: '/category/list',
        name: '/category/list',
        meta: {
            title: '分类列表'
        },
        component: CateGory
    }, {
        path: '/order/list',
        name: '/order/list',
        meta: {
            title: '订单列表'
        },
        component: Order
    }, {
        path: '/comment/list',
        name: '/comment/list',
        meta: {
            title: '评价列表'
        },
        component: Comment
    }, {
        path: '/image/list',
        name: '/image/list',
        meta: {
            title: '图库列表'
        },
        component: Image
    }, {
        path: '/notice/list',
        name: '/notice/list',
        meta: {
            title: '公告列表'
        },
        component: Notice
    }, {
        path: '/setting/base',
        name: '/setting/base',
        meta: {
            title: '配置列表'
        },
        component: Setting
    }, {
        path: '/coupon/list',
        name: '/coupon/list',
        meta: {
            title: '优惠卷列表'
        },
        component: Coupon
    }, {
        path: '/user/list',
        name: '/user/list',
        meta: {
            title: '用户列表'
        },
        component: User
    }, {
        path: '/manager/list',
        name: '/manager/list',
        meta: {
            title: '管理员列表'
        },
        component: Manager
    },
    {
        path: '/access/list',
        name: '/access/list',
        meta: {
            title: '菜单权限'
        },
        component: Access
    },
    {
        path: '/role/list',
        name: '/role/list',
        meta: {
            title: '角色管理'
        },
        component: Role
    },
    {
        path: '/skus/list',
        name: '/skus/list',
        meta: {
            title: '规格管理'
        },
        component: Skus
    },
    {
        path: '/level/list',
        name: '/level/list',
        meta: {
            title: '会员等级'
        },
        component: Level
    }, {
        path: "/setting/buy",
        name: "/setting/buy",
        component: SettingBuy,
        meta: {
            title: "支付设置"
        }
    }, {
        path: "/setting/ship",
        name: "/setting/ship",
        component: SettingShip,
        meta: {
            title: "物流设置"
        }
    }, {
        path: "/distribution/index",
        name: "/distribution/index",
        component: DistributionIndex,
        meta: {
            title: "分销员管理"
        }
    }, {
        path: "/distribution/setting",
        name: "/distribution/setting",
        component: DistributionSetting,
        meta: {
            title: "分销设置"
        }
    }
]

export const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 动态添加路由
export function addRoutes(menus) {
    // 是否有新的路由
    let hasNewRoutes = false

    const findAndRoutesByMenus = (arr) => {
        arr.forEach(e => {
            let item = asyncRoutes.find(o => o.path == e.frontpath)
            if (item && !router.hasRoute(item.path)) {
                router.addRoute("admin", item)
                hasNewRoutes = true
            }
            if (e.child && e.child.length > 0) {
                findAndRoutesByMenus(e.child)
            }
        })
    }
    findAndRoutesByMenus(menus)

    return hasNewRoutes
}